<?php

/**
 * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 * Copyright (c) 2010 Mr. Ynk: http://www.mrynk.nl
 */

/**
 * @author Michel Ypma: michel@mrynk.nl
 */

require_once 'schema/SchemaUtils.php';


class SchemaLoader
{
    private $filenames;
    private $xmls;
    private $classes;


    public function __construct($filenames)
    {
        if (!is_array($filenames))
        {
            $arr                = array($filenames);
            $filenames          = $arr;
        }

        $this->filenames        = $filenames;
        $this->xmls             = array();
        $this->classes          = array();

        $this->parseXML();
    }

    public function getClasses()
    {
        return $this->classes;
    }

    public function getClassByName($name)
    {
        foreach ($this->classes as $class)
        {
            if ($class['class']['name'] == $name)
            {
                return $class;
            }
        }
    }

    private function parseXML()
    {
        foreach ($this->filenames as $filename)
        {
            $contents = file_get_contents($filename);

            MytoCLI::output(':: PARSING: '.$filename);

            $this->xmls[] = new SimpleXMLElement($contents);
        }

        foreach ($this->xmls as $xml)
        {
            foreach ($xml->classes as $cgroup)
            {
                foreach ($cgroup->class as $class)
                {
                    if ($class['ns'])
                    {
                        $ns = $class['ns'];
                    }
                    else
                    {
                        $ns = $cgroup['ns'];
                    }

                    if ($class['base_class_ns'] != '!' && $class['base_class_ns'])
                    {
                        $base_class_ns = $class['base_class_ns'];
                    }
                    elseif ($class['base_class_ns'] != '!' && $cgroup['base_class_ns'])
                    {
                        $base_class_ns = $cgroup['base_class_ns'];
                    }
                    else
                    {
                        $base_class_ns = false;
                    }

                    $import = array();

                    foreach ($class->attribute as $attribute)
                    {
                        foreach ($xml->classes as $cgroup2)
                        {
                            foreach ($cgroup2->class as $class2)
                            {
                                if ((string) $attribute['type'] == (string) $class2['name'])
                                {
                                    $import[] = $class2['name'];
                                }
                            }
                        }
                    }

                    $this->classes[] = array(
                        'language'          => $xml->languages->language,
                        'ns'                => $ns,
                        'base_class_ns'     => $base_class_ns,
                        'class'             => $class,
                        'import'            => $import
                    );
                }
            }
        }

        foreach ($this->classes as $ckey => $class)
        {
            foreach ($class['import'] as $ikey => $import)
            {
                foreach ($this->classes as $class)
                {
                    if ($import == (string) $class['class']['name'])
                    {
                        $this->classes[$ckey]['import'][$ikey] = $class['ns'].'.'.$class['class']['name'];
                    }
                }

                $this->classes[$ckey]['import'] = array_unique($this->classes[$ckey]['import']);
            }
        }
    }
}
?>